//
//  VisitorLoginView.swift
//  新浪微博
//
//  Created by 林志国 on 15/9/1.
//  Copyright © 2015年 林志国. All rights reserved.
//

import UIKit
//访客视图
class VisitorLoginView: UIView {

    //设置界面信息
    //imageName:图片名称 message :消息文字
    func setupInfo(imageName:String?,message:String) {
        
        messageLabel.text = message
        //判断是否传递图片 有照片就不是首页
        if let imageName = imageName {
            //给iconView设置图片
            iconView.image = UIImage(named: imageName)
            //同时隐藏小房子图片
            homeIconView.hidden = true
            //将遮罩图层移动到底部
            sendSubviewToBack(maskIconView)
        }else{
            //开启动画
            startAnimation()
        }
        
    }
    
     //首页图标动画
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.duration = 4;
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        
        //设置动画不会被销毁(当view销毁的时候 上面的动画也会一起被销毁)
        anim.removedOnCompletion = false
        //添加动画
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    
    //MARK: - 界面布局
    //纯代码开发会被调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    //SB开发的话会被调用的方法
    required init?(coder aDecoder: NSCoder) {

        super.init(coder:aDecoder)
        
        setupUI()
    }
    
    //设置界面 - 负责添加和设置控件位置
    private func setupUI() {
        //1.添加控件
        addSubview(iconView)
        addSubview(homeIconView)
        addSubview(maskIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //2.设置布局(是将布局添加到视图上)
        // "view1.attr1 = view2.attr2 * multiplier + constant"
        //item: <#T##AnyObject#>的 attribute: <#T##NSLayoutAttribute#> = (relatedBy: <#T##NSLayoutRelation#>) toItem: <#T##AnyObject?#> 的 attribute: <#T##NSLayoutAttribute#> * multiplier: <#T##CGFloat#> + constant: <#T##CGFloat#>
        // 默认情况下，使用纯代码开发，是不支持自动布局的，如果要支持自动布局，需要将
        // 控件的 translatesAutoresizingMaskIntoConstraints 设置为 false / NO
        //1>图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        //2>小房子 代码设计的时候最好有一个固定的参照物(都和上一个进行参照)
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //3>文本
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //提示:如果要设置为固定值 那么需要将参照属性设置为NSLayoutAttribute.NotAnAttribute 也就是nil的意思
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        //4>注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //5>登陆按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //6>遮罩视图 - 用 VFL(可视化布局语言) 实现
        /*
            H: 水平方向
            V: 垂直方向
            | 边界
            [] 控件
        
            metrics: nil 极少用
            views: [key, VFL 中[] 括起的名称, value: 控件] -> 控件和 VFL 的映射

        */
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        let cons = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v" : maskIconView])
        //VFL的技巧
        addConstraints(cons)
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-35)-[regButton]", options: [], metrics: nil, views: ["v": maskIconView, "regButton": registerButton]))
        
        //设置背景颜色 灰度图 r = g = b
        //提高程序性能的一个技巧:能用颜色表示就不用图片
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1.0)
    }
    
//MARK:懒加载View里的控件 !!懒加载只负责创建控件!!
    //图标
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //小房子
    private lazy var homeIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //遮罩视图 不能使用maskView这个名字 因为是系统的命名
    private lazy var maskIconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //消息文字
    private lazy var messageLabel : UILabel = {
        let label = UILabel()
        
        label.text = "客官~~快进来看看吧~~都是漂亮大姑娘呀~~~"
        label.textColor = UIColor.darkGrayColor()//一般不用纯黑色
        label.textAlignment = NSTextAlignment.Center //设置文字居中
        label.numberOfLines = 0//设置行数为0
        label.font = UIFont.systemFontOfSize(14)//设置字体
        
        return label
    }()
    
    //注册按钮
     lazy var registerButton : UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", forState: UIControlState.Normal)//设置文字
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)//设置文字颜色
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return button
    }()
    
    //登录按钮
    lazy var loginButton :UIButton = {
        let button = UIButton()
        
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor .darkGrayColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
    
        return button
    }()
    

}
